/**     
* @Title: TrashDataController.java
* @Package me.springboot.mybatis.web
* @Description: TODO
* @author zengh 
* @date 2018/04/16
* @version V1.0     
*/
package me.springboot.mybatis.web;

import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.model.TrashData;
import me.springboot.mybatis.service.TrashDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**   
* @ClassName: TrashDataController   
* @Description: TODO
* @author zengh
* @date 2018/04/16
*/
@RestController
@RequestMapping("/trash/data")
@CrossOrigin
public class TrashDataController {

    @Resource
    private TrashDataService trashDataService;

    /**
	* @Title: add   
	* @Description: 添加
	* @param trashData
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/add")
    public RetResult<Object> add(TrashData trashData) {
        trashDataService.save(trashData);
        return RetResponse.makeOKRsp();
    }
    
    /**
	* @Title: add2   
	* @Description: 传入参数为JSON格式
	* @param trashData
	* @Reutrn RetResult<Object>
	*/
	@PostMapping("/add2")
	public RetResult<Object> add2(@RequestBody TrashData trashData) {
		trashDataService.save(trashData);
		return RetResponse.makeOKRsp();
	}

    /**
	* @Title: delete   
	* @Description: 删除
	* @param trashData
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) {
        trashDataService.deleteById(id);
        return RetResponse.makeOKRsp();
    }

    /**
	* @Title: update   
	* @Description: 更新
	* @param trashData
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/update")
    public RetResult<Object> update(TrashData trashData) {
        trashDataService.update(trashData);
        return RetResponse.makeOKRsp();
    }
    
    /**
	* @Title: update   
	* @Description: 更新(传入参数为JSON格式)
	* @param trashData
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody TrashData trashData) {
        trashDataService.update(trashData);
        return RetResponse.makeOKRsp();
    }

    /**
	* @Title: detail   
	* @Description: 根据ID查询详情
	* @param trashData
	* @Reutrn RetResult<TrashData>
	*/
    @PostMapping("/detail")
    public RetResult<TrashData> detail(@RequestParam Integer id) {
        TrashData trashData = trashDataService.findById(id);
        return RetResponse.makeOKRsp(trashData);
    }
    
    /**
	* @Title: detail   
	* @Description: 传入参数为JSON格式
	* @param reqMap
	* @Reutrn RetResult<TrashData>
	*/
	@PostMapping("/detail2")
	public RetResult<TrashData> detail(@RequestBody Map<String, Object> reqMap) {
		Integer id = Integer.valueOf(reqMap.get("id").toString());
		TrashData trashData = trashDataService.findById(id);
        return RetResponse.makeOKRsp(trashData);
	}

    /**
	* @Title: list   
	* @Description: 分页查询
	* @param page 页码
	* @param size 每页条数
	* @Reutrn RetResult<PageInfo<TrashData>>
	*/
    @PostMapping("/list")
    public RetResult<PageInfo<TrashData>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TrashData> list = trashDataService.findAll();
        PageInfo<TrashData> pageInfo = new PageInfo<TrashData>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }  
}